#!/bin/bash
set -euE
shopt -s extglob nullglob

${ROOTCMD} dpkg -s > "${CLOUD_BUILD_OUTPUT_DIR}/${CLOUD_BUILD_NAME}.dpkg-status"

echo -n ${PARTUUID_ROOT} > "${CLOUD_BUILD_OUTPUT_DIR}/${CLOUD_BUILD_NAME}.data.root.partuuid"
echo -n ${PARTUUID_ESP} > "${CLOUD_BUILD_OUTPUT_DIR}/${CLOUD_BUILD_NAME}.data.efi.partuuid"
echo -n ${FSUUID_ROOT} > "${CLOUD_BUILD_OUTPUT_DIR}/${CLOUD_BUILD_NAME}.data.root.fsuuid"

exec > ${CLOUD_BUILD_OUTPUT_DIR}/${CLOUD_BUILD_NAME}.info

function show() {
  if [ ${#@} -ge 1 ]; then
    tail -vn +1 "$@" | sed -e "s:${FAI_ROOT}::"
  fi
}

echo "--- CLOUD RELEASE ---"
cat ${FAI_ROOT}/etc/cloud-release
echo "--- END CLOUD RELEASE --- "
echo "--- APT SOURCES.LIST ---"
if [ -n "${FAI_ROOT}" ] ; then
	if [ -r ${FAI_ROOT}/etc/apt/sources.list ] ; then
		show ${FAI_ROOT}/etc/apt/sources.list
	fi
	if [ -d "${FAI_ROOT}/etc/apt/sources.list.d" ] ; then
		for i in $(find ${FAI_ROOT}/etc/apt/sources.list.d -type f) ; do
			show ${i}
		done
	fi
fi
echo "--- END APT SOURCES.LIST ---"
echo "--- APT PREFERENCES ---"
if [ -n "${FAI_ROOT}" ] ; then
	if [ -r "${FAI_ROOT}/etc/apt/preferences" ] ; then
		show ${FAI_ROOT}/etc/apt/preferences
	fi
	if [ -d "${FAI_ROOT}/etc/apt/preferences.d" ] ; then
		for i in $(find ${FAI_ROOT}/etc/apt/preferences.d -type f) ; do
			show ${i}
		done
	fi
fi
echo "--- END APT PREFERENCES ---"
echo "--- FILES CHANGED ---"
debsums -r ${FAI_ROOT} -a -c | sed -e "s:^${FAI_ROOT}::" || [ $? != 2 ]
echo "--- END FILES CHANGED ---"
echo "--- PACKAGES ---"
${ROOTCMD} dpkg -l
echo "--- END PACKAGES ---"
